package com.wanmei.controller.product;

import com.wanmei.exception.WanmeiException;
import com.wanmei.pojo.*;
import com.wanmei.service.AttributeValueService;
import com.wanmei.service.CategoryService;
import com.wanmei.service.Impl.CategoryServiceImpl;
import com.wanmei.service.Impl.ProductServiceImpl;
import com.wanmei.service.ProductKuidService;
import com.wanmei.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class EditProductController {
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private ProductKuidService productKuidService;
    @Autowired
    private AttributeValueService attributeValueService;

    /**
     * 产品编辑
     *
     * @param pid
     * @param cid
     * @return
     */
    @RequestMapping("/productedit")
    public ModelAndView productEdit(@RequestParam(value = "pid") int pid, @RequestParam(value = "cid") int cid) {
        ModelAndView mv = new ModelAndView();
        //根据pid查询产品信息
        List<Product> productList = productService.selectProductInfoByPid(pid);
        //根据pid查询库存信息
        //库存
        List<ProductStoreNum> listProductNum = productKuidService.getProductStoreNum(pid);

        //伪造库存
        if (listProductNum == null || listProductNum.isEmpty()) {
            for (int i = 1; i <= 5; i++) {
                ProductStoreNum productStoreNum = new ProductStoreNum();
                Productkuid productkuid = new Productkuid();
                productkuid.setVid(i);
                productkuid.setPid(pid);
                productkuid.setStorenum(10);
                productKuidService.insertBydefault(productkuid);
                String vname = attributeValueService.getVnameByVid(i);
                productStoreNum.setVid(i);
                productStoreNum.setStoreNum(10);
                productStoreNum.setVname(vname);
                listProductNum.add(productStoreNum);
            }
        }
        mv.addObject("listProductNum", listProductNum);

        //根据cid查询类别
        List<Category> categoryList = categoryService.getCategoryByCid(cid);
        mv.addObject("productlist", productList);
        mv.addObject("productKuidlist", listProductNum);
        mv.addObject("categoryList", categoryList);
        mv.setViewName("/product/productEdit");
        return mv;
    }


    @RequestMapping("/producteditsubmit")
    @ResponseBody
    public JsonResult productEditSubmit(Product product, HttpServletRequest req) throws WanmeiException {

        String[] vids = req.getParameterValues("vid");
        String[] storenums =req.getParameterValues("storeNum") ;

        JsonResult result = new JsonResult();

        String realPath = req.getSession().getServletContext().getRealPath("");

        int isSuccess=productService.updateProductInfo(product,vids,storenums,realPath);

        if(isSuccess==1){
          result.setCode(1);
        }else {
            result.setMsg("修改失败");
        }
        return result;
    }
}
